<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
	<base href="<%=request.getContextPath()%>/">
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/addroom.css"/>
		<script type="text/javascript" src="js/public/jquery-2.1.1.min.js"></script>
	</head>
	<body>
		<div class="addroom">
			<table border="1px" cellspacing="0px" cellpadding="0px" width="100%" height="100%">
				<tr height="10%">
					<th colspan="4">添加房间信息</th>
				</tr>
				<tr>
					<td>房间地址</td>
					<td><input type="text" value="" id="address"/></td>
					<td>房间户型</td>
					<td>
					<select id="rtid">
					<!-- <option></option> -->
					</select>
					
					</td>
				</tr>
				<tr>
					<td>可容纳人数</td>
					<td><input type="text" name="" id="capacity" value="" /></td>
					<td>房间类型</td>
					<td>
					<select name="room-type" id="type">
						<option value="1">男生宿舍</option>
						<option value="2">女生宿舍</option>
					</select></td>
				</tr>
				<tr>
					<td>房租</td>
					<td><input type="text" name="" id="rent" value="" /></td>
					<td>支付方式</td>
					<td><input type="text" name="" id="payway" value="" /></td>
				</tr>
				<tr>
					<td>房东</td>
					<td><input type="text" name="" id="lname" value="" /></td>
					<td>房东电话</td>
					<td><input type="text" name="" id="tel" value="" /></td>
				</tr>
				<tr>
					<td>房间状态</td>
					<td><select name="state" id="state">
						<option value="1">正常</option>
						<option value="2">设施损坏</option>
					</select></td>
					<td>租房日期</td>
					<td><input type="date" name="" id="renttime" value="" /></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="button" name="" id="add" value="添加" /></td>
					<td colspan="2" align="center"><input type="reset" name="" id="" value="取消" /></td>
					
				</tr>
				<tr height="10%">
					<td colspan="4" id="info"></td>
				</tr>
			</table>
		</div>
	</body>
	<script type="text/javascript">
	$(function(){
		$.ajax({
			//查询所有的房间户型
			url:"roomType/findAll",
			type:"get",
			success:function(data){
				$.each(data,function(index,val){
					var option=$("<option></option>");
					option.val(val.id);
					option.text(val.type);
					$("#rtid").append(option);
				});
			}
		});
		//添加房间信息
		$("#add").click(function(){
			var address=$("#address").val();
			var capacity=$("#capacity").val();
			var rent=$("#rent").val();
			var payway=$("#payway").val();
			var lname=$("#lname").val();
			var tel=$("#tel").val();
			if(address.trim()==""){
				alert("房间地址不能为空");
				return;
			}
			if(parseInt(capacity.trim())==0||capacity.trim()==""){
				alert("房间容量必须填写");
				return;
			}
			
			if(parseInt(rent)==0||rent.trim()==""){
				alert("房间租金必须填写");
				return;
			}
			if(payway.trim()==""){
				alert("支付方式必须填写");
				return;
			}
			if(lname.trim().length<2||lname.trim().length>8){
				alert("房东姓名必须在2-8位间");
				return;
			}
			var regex_tel=/^1[3,4,5,7,8,9][0-9]{9}$/;
			if(!regex_tel.test(tel)){
				alert("电话号码格式不对");
				return;
			}
			//验证房间信息
			$.ajax({
				url:"room/add",
				data:{"address":$("#address").val(),
				      "rtid":$("#rtid option:selected").val(),
				      "capacity":$("#capacity").val(),
				      "type":$("#type option:selected").val(),
				      "rent":$("#rent").val(),
				      "payway":$("#payway").val(),
				      "lname":$("#lname").val(),
				      "tel":$("#tel").val(),
				      "state":$("#state option:selected").val(),
				      "renttime":$("#renttime").val()
				},
				type:"post",
				success:function(data){
					if(data==0){
						$("#info").html("<font color=red>添加失败</font>");
					}
					else if(data==1){
						$("#info").html("<font color=red>房间户型不存在，请重新选择！</font>");
					}
					else if(data==2){
						$("#info").html("<font color=green>添加成功</font>");
					}
				}
			});
		});
	})
	</script>
</html>
